<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" top="10vh" :width="lang === 'zh'?'480px':'660px'" @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" :label-width="lang === 'zh'?'80px':'220px'" :rules="rules" label-position="left" style="margin:0 20px 0 30px;">
        <el-form-item :label="$t('名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="name" style="width: 200px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('IP类型')" prop="ip_type">
          <el-radio style="margin-left:20px;" v-model="record.ip_type" label="ipv4" class="ipv4">IPv4</el-radio>
          <el-radio v-model="record.ip_type" label="ipv6" class="ipv6">IPv6</el-radio>
        </el-form-item>
        <el-form-item :label="$t('源IP')" prop="src">
          <el-select v-model="record.src" ref="srcIp" size='small' filterable clearable style="width: 200px;">
            <el-option v-for="item in filMac(srcList)" :key="item.name" :label="item.nameShow" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('目的IP')" prop="dst">
          <el-select v-model="record.dst" ref="dstIp" size='small' filterable clearable style="width: 200px;">
            <el-option v-for="item in filMac(dstList)" :key="item.name" :label="item.nameShow" :value="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('目的端口号')" prop="dport">
          <el-input v-emoji v-model="record.dport" ref="dport" style="width: 200px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('并发类型')" prop="paral_type">
          <el-radio style="margin-left:20px;" v-model="record.paral_type" label="each" class="eachIp">{{$t('每IP并发')}}</el-radio>
          <el-radio v-model="record.paral_type" label="all" class="allIp">{{$t('总并发')}}</el-radio>
        </el-form-item>
        <el-form-item :label="$t('并发方向')" prop="dir_type">
          <el-radio style="margin-left:20px;" v-model="record.dir_type" label="SRC" class="SRC">{{$t('源方向')}}</el-radio>
          <el-radio v-model="record.dir_type" label="DST" class="DST">{{$t('目的方向')}}</el-radio>
        </el-form-item>
        <el-form-item :label="$t('并发连接数')" prop="count">
          <el-input v-emoji v-model="record.count" ref="count" style="width: 200px;" size='small'></el-input>
          <el-tooltip class="item" effect="light" :content="$t('并发连接数填写太小可能导致设备无法连接')" placement="top">
            <i class="el-icon-warning-outline" style="color:#FF0000;font-size:16px;"></i>
          </el-tooltip>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addConnect, editConnect } from '@/api/safe/connect'
// import { getAddrGroupArray } from '@/api/list/addr-group'
import { getAddrArray } from '@/api/list/addr'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'ConnectDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateCount = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (isNaN(Number(value))) {
        callback(new Error(this.$t('请输入数字')))
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } if (Number(value) < 0) {
        callback(new Error(this.$t('不能输入负数')))
      } else if (Number(value) > 1000000) {
        callback(new Error(this.$t('并发连接数不能大于1百万')))
      } else {
        callback()
      }
    }
    var validatePost = (rule, value, callback) => {
      if (value) {
        if (isNaN(Number(value))) {
          callback(new Error(this.$t('请输入数字')))
        } else if (Number(value) > 65535 || Number(value) < 0) {
          callback(new Error(this.$t('端口的范围为0~65535')))
        } else if (!this.isInteger(value)) {
          callback(new Error(this.$t('只能输入整数')))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      lang: this.$i18n.locale,
      record: {
        id: '',
        name: '',
        ip_type: 'ipv4',
        paral_type: 'each', // each/all
        dir_type: 'SRC', // SRC/DST
        count: '',
        src: '',
        dst: '',
        dport: ''
      },
      recordCopy: {
        id: '',
        name: '',
        ip_type: 'ipv4',
        paral_type: 'each', // each/all
        dir_type: 'SRC', // SRC/DST
        count: '',
        src: '',
        dst: '',
        dport: ''
      },
      srcList: [],
      dstList: [],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        ip: [{ required: false, validator: validateName, trigger: 'blur' }],
        mac: [{ required: false, validator: validateName, trigger: 'blur' }],
        count: [{ required: false, validator: validateCount, trigger: 'blur' }],
        dport: [{ required: false, validator: validatePost, trigger: 'blur' }]
      }
    }
  },
  methods: {
    filMac(list=[]) { 
      return list.filter(item => item.ip_type !== 7) 
    },
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['name', 'dport', 'count']
        const selectData = ['srcIp', 'dstIp']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
      this.record = Object.assign(this.record, data)
      this.srcList = []
      this.dstList = []
      getAddrArray().then(response => {
        if (response.status === 0) {
          for (let item of response.info) {
            item.nameShow = this.$t('地址-') + item.name
            this.srcList.push(item)
            this.dstList.push(item)
          }
          // getAddrGroupArray().then(res => {
          //   if (res.status === 0) {
          //     for (let item of res.info) {
          //       item.nameShow = this.$t('地址组-') + item.name
          //       this.srcList.push(item)
          //       this.dstList.push(item)
          //     }
          //   }
          // })
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.record.id) {
            editConnect(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            addConnect(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
